package com.jhd.fuzhou.util;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.service.ReceiveService;
import com.jhd.fuzhou.view.login.FuzhouApplication;
import com.jhd.fuzhou.vo.JsonForResult;
import com.jhd.fuzhou.vo.ReceiveReportVo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class AddReadPerson {
    public static void addReadPerson(FuzhouApplication application, Context context,String json){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        ReceiveService vs = retrofit.create(ReceiveService.class);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"), json);
        Call<JsonForResult> jsonForResultCall = vs.addReadPerson(application.getAccessToken(),requestBody);
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {

                    } else {
                        CustomToast.showText(context, jsonForResult.getMessage(), 3000, 0, 0).show();
                    }
                } catch (Exception e) {
                    UtilHelper.alertMsg(context, "提交阅文人错误!", e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "提交阅文人错误!";
                UtilHelper.alertMsg(context, "提交阅文人错误!", msg);
            }
        });
    }


    public static void postMessage(FuzhouApplication application, Context context, ReceiveReportVo receiveReportVo, String checkPerson){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        ReceiveService vs = retrofit.create(ReceiveService.class);
        Map<String, Object> params = new LinkedHashMap<String, Object>();
        params.put("id",receiveReportVo.getId());
        params.put("username",application.getUserName());
        params.put("checkPerson",checkPerson);
        params.put("messageIsOpen",0);
        List<String> list=new ArrayList<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"), JSON.toJSONString(params));
        Call<JsonForResult> jsonForResultCall = vs.addReadPerson(application.getAccessToken(),requestBody);
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {

                    } else {
                        CustomToast.showText(context, jsonForResult.getMessage(), 3000, 0, 0).show();
                    }
                } catch (Exception e) {
                    UtilHelper.alertMsg(context, "发送短信失败!", e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "发送短信失败!";
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }

}
